<template>
  <div style="height: 100%">
    <div class="primaty" v-show="result === ''" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/bg.jpg' + ')'}">
      <div class="pri-bg" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/bg-1.png' + ')'}">
        <img class="pri-top" :src="cdnUrl + '/assets/primary/' + pageNum + '.png'" alt="" />
        <div class="pri-problem" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/problem.png' + ')'}">
          <div class="pro-top" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/title.png' + ')'}">
            <p>{{pageNum}}/8</p>
          </div>
          <div class="pro-list">
            <div class="list">
              <transition name="fade">
                <p v-show="textFlag">{{list[pageNum - 1].text}}</p>
              </transition>
            </div>
          </div>
        </div>
        <div class="pri-btn">
          <div class="btn" @click="toChoose('0')">
            <img :src="chooseFlag === '0'? cdnUrl + '/assets/primary/wrong-show.png': cdnUrl + '/assets/primary/wrong-hide.png'" alt="" />
          </div>
          <div class="btn" @click="toChoose('1')">
            <img :src="chooseFlag === '1'? cdnUrl + '/assets/primary/right-show.png': cdnUrl + '/assets/primary/right-hide.png'" alt="" />
          </div>
        </div>
      </div>
      <transition name="fade">
        <div class="pri-pop" v-show="popFlag !== 2">
        <div class="pop" v-show="popFlag === 1 && !list[pageNum - 1].des" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/right-bg1.png' + ')'}">
          <img class="pop-top" :src="cdnUrl + '/assets/primary/right-top.png'" alt="" />
          <img class="pop-result" :src="cdnUrl + '/assets/primary/right.png'" alt="" />
          <div class="pop-btn" @click="goNext">
            <img :src="pageNum === 8? cdnUrl + '/assets/high/true.png' : cdnUrl + '/assets/primary/next.png'" alt="" />
          </div>
          <img class="pop-bottom" :src="cdnUrl + '/assets/primary/right-bottom.png'" alt="" />
        </div>
        <div class="pop right2" v-show="popFlag === 1 && list[pageNum - 1].des" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/right-bg2.png' + ')'}">
          <img class="pop-top" :src="cdnUrl + '/assets/primary/right-top.png'" alt="" />
          <img class="pop-result" :src="cdnUrl + '/assets/primary/right.png'" alt="" />
          <p>{{list[pageNum - 1].des}}</p>
          <div class="pop-btn" @click="goNext">
            <img :src="pageNum === 8? cdnUrl + '/assets/high/true.png' : cdnUrl + '/assets/primary/next.png'" alt="" />
          </div>
          <img class="pop-bottom" :src="cdnUrl + '/assets/primary/right-bottom.png'" alt="" />
        </div>
        <div class="pop wrong1" v-show="popFlag === 0 && !list[pageNum - 1].des" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/wrong-bg1.png' + ')'}">
          <img class="pop-top" :src="cdnUrl + '/assets/primary/wrong-top.png'" alt="" />
          <img class="pop-result" :src="cdnUrl + '/assets/primary/wrong.png'" alt="" />
          <div class="pop-btn" @click="goNext">
            <img :src="pageNum === 8? cdnUrl + '/assets/high/true.png' : cdnUrl + '/assets/primary/next.png'" alt="" />
          </div>
          <img class="pop-bottom" :src="cdnUrl + '/assets/primary/wrong-bottom.png'" alt="" />
        </div>
        <div class="pop wrong2" v-show="popFlag === 0 && list[pageNum - 1].des" :style="{backgroundImage: 'url('+ cdnUrl + '/assets/primary/wrong-bg2.png' + ')'}">
          <img class="pop-top" :src="cdnUrl + '/assets/primary/wrong-top.png'" alt="" />
          <img class="pop-result" :src="cdnUrl + '/assets/primary/wrong.png'" alt="" />
          <p>{{list[pageNum - 1].des}}</p>
          <div class="pop-btn" @click="goNext">
            <img :src="pageNum === 8? cdnUrl + '/assets/high/true.png' : cdnUrl + '/assets/primary/next.png'" alt="" />
          </div>
          <img class="pop-bottom" :src="cdnUrl + '/assets/primary/wrong-bottom.png'" alt="" />
        </div>
      </div>
      </transition>
    </div>
    <Result v-if="result !== ''" :result="result" @goReturns="goReturns"></Result>
  </div>
</template>

<script>
import { getProList } from '../../utils/index'
import Result from './Result'
import { getAnswerResult, getResult } from '../../service'
import { cdnUrl } from '../../service/env'

export default {
  name: 'Primary',
  props: {
    id: String,
    goReturn: Function
  },
  data () {
    return {
      list: getProList(1),
      pageNum: 1,
      chooseFlag: '2',
      popFlag: 2,
      clickFlag: true,
      result: '',
      textFlag: false,
      cdnUrl: cdnUrl
    }
  },
  components: {
    Result
  },
  mounted () {
    setTimeout(() => {
      this.textFlag = true
    }, 100)
  },
  methods: {
    // 点击选择
    toChoose (type) {
      if (this.clickFlag) {
        this.clickFlag = false
        this.chooseFlag = type
        getAnswerResult(this.id, type, this.list[this.pageNum - 1].id).then((data) => {
          this.clickFlag = true
          if (data.success === 'true') {
            this.popFlag = data.data.code
          }
        }).catch(() => {
          this.clickFlag = true
        })
      }
    },
    // 下一题
    goNext () {
      if (this.pageNum < 8) {
        this.textFlag = false
        this.pageNum++
        this.chooseFlag = '2'
        this.popFlag = 2
        setTimeout(() => {
          this.textFlag = true
        }, 250)
      } else {
        if (this.clickFlag) {
          this.clickFlag = false
          getResult(this.id).then((data) => {
            this.clickFlag = true
            this.result = data.success
          })
        }
      }
    },
    // 再来一次
    goReturns () {
      this.$emit('goReturn')
    }
  }
}
</script>

<style scoped lang="scss">
.fade-enter-active{
  transition: opacity 0.25s ease-out;
}
.fade-enter{
  opacity: 0;
}
.primaty{
  width: 100%;
  height: 100%;
  // background: url("../../assets/primary/bg.jpg") no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  .pri-bg{
    width: 92%;
    height: 450px;
    // background: url("../../assets/primary/bg-1.png") no-repeat;
    background-size: 100% 100%;
    margin: 18px auto 0;
    overflow: hidden;
    .pri-top{
      width: 40.6%;
      height: 102px;
      margin: 8px auto 40px;
    }
    .pri-problem{
      width: 93%;
      height: 178px;
      margin: 0 auto;
      // background: url("../../assets/primary/problem.png") no-repeat;
      background-size: 100% 100%;
      position: relative;
      .pro-top{
        width: 42%;
        height: 25.5px;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: -7px auto 0;
        // background: url("../../assets/primary/title.png") no-repeat;
        background-size: 100% 100%;
        p{
          font-size: 14px;
          color: #000000;
          text-align: center;
          line-height: 21px;
          font-weight: 600;
        }
      }
      .pro-list{
        width: 78.6%;
        height: 100%;
        margin: 0 auto;
        overflow: hidden;
        .list{
          width: 100%;
          height: 100%;
          margin: 0 auto 0;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        p{
          font-size: 14px;
          color: #000000;
          font-weight: 600;
        }
      }
    }
    .pri-btn{
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-top: 23px;
      .btn{
        width: 41.1%;
        margin-bottom: 10px;
        img{
          width: 100%;
        }
      }
    }
  }
  .pri-pop{
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    .pop{
      width: 86.6%;
      height: 177px;
      // background: url("../../assets/primary/right-bg1.png");
      background-size: 100% 100%;
      position: relative;
      .pop-top{
        width: 42.3%;
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        margin: -7px auto 0;
      }
      .pop-result{
        width: 50.2%;
        margin: 60px auto 0;
      }
      p{
        font-size: 13px;
        color: #000000;
        font-weight: 600;
        text-align: center;
        margin-top: 19px;
      }
      .pop-btn{
        width: 80px;
        height: 24px;
        margin: 40px auto 0;
        img{
          width: 100%;
          height: 100%;
        }
      }
      .pop-bottom{
        width: 22.6%;
        position: absolute;
        right: 20px;
        bottom: 9px;
      }
    }
    .right2 {
      height: 220px;
      // background: url("../../assets/primary/right-bg2.png");
      background-size: 100% 100%;
      .pop-result{
        margin: 50px auto 0;
      }
      .pop-bottom{
        bottom: 11px;
      }
    }
    .wrong1{
      height: 210px;
      // background: url("../../assets/primary/wrong-bg1.png");
      background-size: 100% 100%;
    }
    .wrong2{
      height: 250px;
      // background: url("../../assets/primary/wrong-bg2.png");
      background-size: 100% 100%;
    }
  }
}
</style>
